package com.api.shopapi.controller;

import com.alibaba.fastjson.JSONObject;
import com.api.shopapi.entity.*;
import com.api.shopapi.entity.dto.JKXueyaDTO;
import com.api.shopapi.service.JKXueyaService;
import com.api.shopapi.service.StaffService;
import com.api.shopapi.util.DateUtil;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.UUID;

@RequestMapping("JKXueya")
@RestController
public class JKXueyaController extends BaseController{

    @Autowired
    @Qualifier("JKXueyaServiceImplOne")
    private JKXueyaService jkXueyaService;

    @Autowired
    @Qualifier("StaffServiceImplOne")
    private StaffService staffService;

    //手动添加用户血压检查记录
    @RequestMapping(value = "Add",method = RequestMethod.POST)
    public MyResult Add(@RequestBody JSONObject jsonObject) {
        try{
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");

            PreCheck preCheck = checkSignFrontInterface(timeStr, sign,"/JKXueya/Add");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }

            String userCode = jsonObject.getString("userCode");
            String gaoYa = jsonObject.getString("gaoYa");
            String diYa = jsonObject.getString("diYa");
            String xinLv = jsonObject.getString("xinLv");
            int fromType = jsonObject.getInteger("fromType");
            Date inspectTime = jsonObject.getDate("inspectTime");

            JKXueya jkXueya=new JKXueya();
            jkXueya.setJKXueyaCode(UUID.randomUUID().toString());
            jkXueya.setUserCode(userCode);
            jkXueya.setGaoYa(gaoYa);
            jkXueya.setDiYa(diYa);
            jkXueya.setXinLv(xinLv);
            jkXueya.setFromType(fromType);
            jkXueya.setInspectTime(inspectTime);
            jkXueya.setCreateTime(new Date());
            jkXueyaService.insert(jkXueya);

            Object data = new Object[] { };
            return success(data);
        }catch (Exception e){
            insertError("/JKXueya/Add", jsonObject.toJSONString(), getExceptionMsg(e), "api");
            return anomalous(getExceptionMsg(e));
        }
    }


    //设备自动添加用户血压检查记录
    @RequestMapping(value = "DeviceAdd",method = RequestMethod.POST)
    public MyResult DeviceAdd(@RequestBody JSONObject jsonObject) {
        try{
            String userCode = jsonObject.getString("userCode");
            String gaoYa = jsonObject.getString("gaoYa");
            String diYa = jsonObject.getString("diYa");
            String xinLv = jsonObject.getString("xinLv");

            JKXueya jkXueya=new JKXueya();
            jkXueya.setJKXueyaCode(UUID.randomUUID().toString());
            jkXueya.setUserCode(userCode);
            jkXueya.setGaoYa(gaoYa);
            jkXueya.setDiYa(diYa);
            jkXueya.setXinLv(xinLv);
            jkXueya.setFromType(1);
            jkXueya.setInspectTime(new Date());
            jkXueya.setCreateTime(new Date());
            jkXueyaService.insert(jkXueya);

            Object data = new Object[] { };
            return success(data);
        }catch (Exception e){
            insertError("/JKXueya/DeviceAdd", jsonObject.toJSONString(), getExceptionMsg(e), "api");
            return anomalous(getExceptionMsg(e));
        }
    }





    //用户血压检查记录列表(app)
    @RequestMapping(value = "Page",method = RequestMethod.POST)
    public MyResult Page(@RequestBody JSONObject jsonObject) {
        try{
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");

            PreCheck preCheck = checkSignFrontInterface(timeStr, sign,"/JKXueya/Page");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }

            String userCode = jsonObject.getString("userCode");
            int pageIndex=jsonObject.getInteger("pageIndex");
            int pageSize=jsonObject.getInteger("pageSize");
            String startTime = jsonObject.getString("startTime");
            String endTime = jsonObject.getString("endTime");
            endTime = DateUtil.timeAddDay(endTime, 1);
            PageInfo<JKXueya> xueyaPageInfo = jkXueyaService.getPageList(pageIndex, pageSize, userCode,startTime,endTime);

            return success(xueyaPageInfo);
        }catch (Exception e){
            insertError("/JKXueya/Page", jsonObject.toJSONString(), getExceptionMsg(e), "api");
            return anomalous(getExceptionMsg(e));
        }
    }

    //用户血压检查记录列表
    @RequestMapping(value = "PageAdm",method = RequestMethod.POST)
    public MyResult PageAdm(@RequestBody JSONObject jsonObject) {
        try{
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String staffCode = jsonObject.getString("staffCode");

            PreCheck preCheck = checkSignAdmInterface(timeStr, sign,staffCode,"/JKXueya/PageAdm");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }

            String name = jsonObject.getString("name");
            int pageIndex=jsonObject.getInteger("pageIndex");
            int pageSize=jsonObject.getInteger("pageSize");
            String startTime = jsonObject.getString("startTime");
            String endTime = jsonObject.getString("endTime");
            endTime = DateUtil.timeAddDay(endTime, 1);

            Staff staff = staffService.getByStaffCode(staffCode);

            PageInfo<JKXueyaDTO> xueyaDTOPageInfo = jkXueyaService.getAdmPageList(pageIndex, pageSize, name, staff.getGovernmentCode(), startTime, endTime);

            return success(xueyaDTOPageInfo);
        }catch (Exception e){
            insertError("/JKXueya/PageAdm", jsonObject.toJSONString(), getExceptionMsg(e), "api");
            return anomalous(getExceptionMsg(e));
        }
    }

}
